<?php
namespace app\usercp\controller;

use app\usercp\service\rbac\Users\Service;
use app\usercp\model\ManageUser;
use think\facade\Cache;
use think\facade\Env;
use think\facade\Session;
use think\Paginator;
use think\paginator\driver\Bootstrap;
use think\Request;
use think\response\Json;

class Finance extends Usercp
{
    protected function initialize() {
        parent::initialize();
        $this->service = model('custom/CustomFee', 'service');
    }

    public function index(Request $request){
        $customs = model('custom/custom')->where(['parent_current_id' => $this->custom->current_id,'status' => 1])
            ->whereOr(['current_id' => $this->custom->current_id])
            ->select();
        foreach ($customs as $k => &$v) {
            if($v['current_id'] == $this->custom->current_id) {
                $v['nickname'] = '我的';
                continue;
            }
            $custom_ids[] = $v['current_id'];
        }
        $this->assign('customs',$customs);

        if ($request->isAjax()){
            $custom_ids = [];
            foreach ($customs as $k => &$v) {
                $custom_ids[] = $v['current_id'];
            }

            $maps = [];
            if (is_null($request->param('current_id')) || $request->param('current_id') == -1){
                $maps[] = ['current_id' ,'in',$custom_ids];
            } else {
                $maps['current_id'] = $request->param('current_id');
            }
            $list = $this->service->lists($maps,'create_time DESC',$this->page,$this->limit,true,['custom']);
            if ($list->isEmpty()){
                return $this->result('',0,'暂无数据');
            }
            foreach ($list as $k => &$v) {
                if ($v['type'] == 'recharge') {
                    $v['money'] = '+'.$v['money'];
                } else {
                    $v['money'] = '-'.$v['money'];
                }
            }
            //注意：此处状态码一定和前端配置的状态码相同，否则数据会出问题(正常返回200，错误返回0)
            return $this->result($list,200);
        }

        return $this->fetch('index');
    }

    public function home(){
        // 我的客户站点统计
        $site_count = [
            'baidu' => 0,
            'wechat' => 0,
            'alipay' => 0,
            'total' => 0,
            'today' => 0,
        ];
        $current_id_arr = model('custom/custom')->where(['parent_current_id' => $this->custom->current_id,'status' => 1])->column('current_id');
        $current_id_arr = array_unique($current_id_arr);
        if ($current_id_arr) {
            $custom_sites = model('site/Site')->where('custom_id' , 'in' ,$current_id_arr)->order('create_time DESC')->select();
        }
        foreach ($custom_sites as $k => $v) {
            $type = explode(',',$v['type']);
            if (in_array('baidu',$type)) {
                $site_count['baidu'] += 1;
            } else if (in_array('wechat',$type)) {
                $site_count['wechat'] += 1;
            } else if (in_array('alipay',$type)) {
                $site_count['alipay'] += 1;
            }
            if (strtotime($v['create_time']) >= strtotime(date('Y-m-d'))) {
                $site_count['today'] += 1;
            }
        }
        $site_count['total'] = count($custom_sites);
        $this->assign('site_count',$site_count);

        // 我的站点
        $mySites = [];
        $where = [];
        $where[] = ['custom_id','=',$this->custom->current_id];
        $where[] = ['status','=',1];
        $where[] = ['deadline','between',[time() ,time()+86400*7]];
        $mySites['renew'] = (int) model('site/Site')->where($where)->count();

        $where = [];
        $where[] = ['custom_id','=',$this->custom->current_id];
        $where[] = ['status','=',1];
        $where[] = ['deadline','<',time()];
        $mySites['invalid'] = (int) model('site/Site')->where($where)->count();
        $this->assign('mySites',$mySites);

        // 公告
        $notices = model('notice/Notice')->field('id,title,content,create_time')->where('status',1)->order('create_time desc')->limit(4)->select();
        $this->assign('notices',$notices);

    	return $this->fetch('home');
    }


}